import os
import shutil

KeyPath = "/home/fan/Doc/Android/"
ProjectPath = os.getcwd()
BuildFilePath = os.path.join(ProjectPath,"android","app","build.gradle")


# 复制文件
# shutil.copy("源文件路径", "目标文件路径")
# print(os.path.join(KeyPath,"key.properties"))

def copy_properties():
    shutil.copy(os.path.join(KeyPath,"key.properties"),os.path.join(ProjectPath,"android"))

def edited_build_file():
    with open(BuildFilePath,"r") as f:
        str = f.read()
        str = str.replace("def localProperties = new Properties()","""
def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

def localProperties = new Properties()""")

        str = str.replace("signingConfig = signingConfigs.debug","""
            signingConfig signingConfigs.release
            ndk {
                //现在国内实行 64位  arm64-v8a
                //noinspection ChromeOsAbiSupport
                abiFilters "arm64-v8a"
            }
        }
        debug {
            signingConfig signingConfigs.debug
            ndk {
                //这里要加上，否则debug包会出问题,后面两个为可选，x86建议加上不然部分模拟器报错
                //noinspection ChromeOsAbiSupport
                abiFilters "armeabi-v7a", "arm64-v8a", "x86"
            }
        }
""")
        # newstr = re.sub(pattern,"=======================================",str)
        # print(str)

        str = str.replace("ndkVersion = flutter.ndkVersion","""
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
        debug {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
""")
    with open(BuildFilePath,"w") as f:
        f.write(str)



copy_properties()
edited_build_file()
